package com.movitech.mobile.task;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.movitech.mobile.cz.base.modules.config.CallBackAbutmentTypeEnum;
import com.movitech.mobile.cz.base.purchase.service.SAbutmentLogsService;
import com.movitech.mobile.cz.base.sys.entity.ext.SCompanyRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SProjectRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SRegionCompanyRequestVO;
import com.movitech.mobile.cz.base.sys.entity.ext.SysUserRequestVO;
import com.movitech.mobile.cz.base.sys.service.SCompanyService;
import com.movitech.mobile.cz.base.sys.service.SProjectService;
import com.movitech.mobile.cz.base.sys.service.SRegionalCompanyService;
import com.movitech.mobile.cz.base.sys.service.SysUserService;
import com.movitech.mobile.mq.RabbitmqConllection;
import com.movitech.mobile.mq.RabbitmqConsumer;
import com.movitech.mobile.task.model.CompanyVoForMQ;
import com.movitech.mobile.task.model.UserMq;
import com.movitech.utils.LoggerManager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 人员MQ消息处理
 *
 * @author ldz
 * @version 1.0.0
 * @description 组织人员岗位消息处理
 * @createTime 2022/03/31
 * @copyright Copyright ©️ 2022 北京魔马科技
 */
@Component
public class OrgAndUserSynRabbitMQConsumer implements RabbitmqConsumer {
    public static LoggerManager log = LoggerManager.get(OrgAndUserSynRabbitMQConsumer.class);

    /**
     * MQ服务地址
     */
    @Value("${mqserver.organduser.url}")
    private String MQ_SERVER_URL;

    /**
     * MQ服务密码
     */
    @Value("${mqserver.organduser.port}")
    private String MQ_SERVER_PORT;

    /**
     * MQ服务账号
     */
    @Value("${mqserver.organduser.user}")
    private String MQ_SERVER_USER;

    /**
     * MQ服务密码
     */
    @Value("${mqserver.organduser.password}")
    private String MQ_SERVER_PASSWORD;

    @Value("${mqserver.organduser.userinfo}")
    private String MQ_QUEUE_USERINFO;
    @Value("${mqserver.organduser.orginfo}")
    private String MQ_QUEUE_ORGINFO;

    @Autowired
    SysUserService sysUserService;
    @Autowired
    SRegionalCompanyService sRegionalCompanyService;
    @Autowired
    SCompanyService sCompanyService;
    @Autowired
    SProjectService sProjectService;
    @Autowired
    @Qualifier("sAbutmentLogsService")
    private SAbutmentLogsService sAbutmentLogsService;


    @Override
    public void init() {
        RabbitmqConllection rabbitmqConllection = RabbitmqConllection
                .instance(MQ_SERVER_URL,
                        MQ_SERVER_PORT,
                        MQ_SERVER_USER,
                        MQ_SERVER_PASSWORD);
        rabbitmqConllection.consumer(MQ_QUEUE_USERINFO, this);
        rabbitmqConllection.consumer(MQ_QUEUE_ORGINFO, this);
    }

    @Override
    public boolean consume(String queueName, String data) {
        if (MQ_QUEUE_ORGINFO.equals(queueName)) {
            try {
                sAbutmentLogsService.saveAbutmentLogs(CallBackAbutmentTypeEnum.CallBackAbutmentTypeEnum_12.getCode(), "", data, 1, 0);
            } catch (Exception ex) {
                log.error("组织人员同步数据保存日志失败", ex);
            }
            // 组织同步
            List<CompanyVoForMQ> companyVoForMQList = null;
            try {
                companyVoForMQList = JSONArray.parseArray(data, CompanyVoForMQ.class);
            } catch (Exception ex) {
            }
            List<SRegionCompanyRequestVO> ListRegion = new ArrayList<>();
            List<SCompanyRequestVO> listOrgRequest = new ArrayList<>();
            List<SProjectRequestVO> listProjectRequest = new ArrayList<>();
            if (companyVoForMQList != null && companyVoForMQList.size() > 0) {
                for (CompanyVoForMQ scompanyRequest : companyVoForMQList) {

                    if (scompanyRequest.getGrade() == null) {
                        continue;
                    }
                    if ("102L".equals(scompanyRequest.getCode())) {
                        SRegionCompanyRequestVO sCompanyRequestVO = new SRegionCompanyRequestVO();
                        sCompanyRequestVO.setRegionCompanyCode(scompanyRequest.getCode());
                        sCompanyRequestVO.setRegionCompanyName(scompanyRequest.getName());
                        sCompanyRequestVO.setRegionCompanyType(1);
                        if (scompanyRequest.getStaus() != null) {
                            sCompanyRequestVO.setStatus(scompanyRequest.getStaus() == 0 ? 1 : 0);
                        }
                        ListRegion.add(sCompanyRequestVO);
                    }
                    // 1区域
                    else if ((4 == scompanyRequest.getGrade() && "102L".equals(scompanyRequest.getFatherCode()) && scompanyRequest.getName().contains("平台"))) {
                        SRegionCompanyRequestVO sCompanyRequestVO = new SRegionCompanyRequestVO();
                        sCompanyRequestVO.setRegionCompanyCode(scompanyRequest.getCode());
                        sCompanyRequestVO.setRegionCompanyName(scompanyRequest.getName());
                        if (scompanyRequest.getStaus() != null) {
                            sCompanyRequestVO.setStatus(scompanyRequest.getStaus() == 0 ? 1 : 0);
                        }
                        ListRegion.add(sCompanyRequestVO);
                    }
                    // 1新的架构 区域   TYZK0102 是代建业务板块
                    else if ("TYZK0102".equals(scompanyRequest.getFatherCode())) {
                        SRegionCompanyRequestVO sCompanyRequestVO = new SRegionCompanyRequestVO();
                        sCompanyRequestVO.setRegionCompanyCode(scompanyRequest.getCode());
                        sCompanyRequestVO.setRegionCompanyName(scompanyRequest.getName()+"（新）");
                        if (scompanyRequest.getStaus() != null) {
                            sCompanyRequestVO.setStatus(scompanyRequest.getStaus() == 0 ? 1 : 0);
                        }
                        ListRegion.add(sCompanyRequestVO);
                    }

                    // 1-代表项目
                    else if (1 == scompanyRequest.getGrade()) {
                        SProjectRequestVO projectRequestVO = new SProjectRequestVO();
                        projectRequestVO.setProjectCode(scompanyRequest.getCode());
                        projectRequestVO.setProjectName(scompanyRequest.getName());
                        projectRequestVO.setFatherCode(scompanyRequest.getFatherCode());
                        if (scompanyRequest.getStaus() != null) {
                            projectRequestVO.setStatus(scompanyRequest.getStaus() == 0 ? 1 : 0);
                        }
                        listProjectRequest.add(projectRequestVO);
                    }// 公司
//                    else if (scompanyRequest.getName().contains("公司")) {
                    else {
                        //其它都作为 公司
                        SCompanyRequestVO sCompanyRequestVO = new SCompanyRequestVO();
                        sCompanyRequestVO.setCompanyCode(scompanyRequest.getId());
                        sCompanyRequestVO.setCompanyName(scompanyRequest.getName());
                        sCompanyRequestVO.setFullCode(scompanyRequest.getFullCode());
                        sCompanyRequestVO.setRemarks(scompanyRequest.getCode());
                        sCompanyRequestVO.setRegionalCompanyId(scompanyRequest.getFatherCode());
                        if (scompanyRequest.getStaus() != null) {
                            sCompanyRequestVO.setStatus(scompanyRequest.getStaus() == 0 ? 1 : 0);
                        }
                        listOrgRequest.add(sCompanyRequestVO);
                    }
                }
                sRegionalCompanyService.batchAdd(ListRegion);
                sCompanyService.batchAdd(listOrgRequest);
                sProjectService.batchAdd(listProjectRequest);
            }
        } else if (MQ_QUEUE_USERINFO.equals(queueName)) {
            sAbutmentLogsService.saveAbutmentLogs(CallBackAbutmentTypeEnum.CallBackAbutmentTypeEnum_13.getCode(), "", data, 1, 0);
            /***人员包含兼岗*/
            List<UserMq> userList = JSONArray.parseArray(data, UserMq.class);
            List<SysUserRequestVO> listUserRequest = new ArrayList<>();
            if (userList != null && userList.size() > 0) {
                for (UserMq user : userList) {
                    SysUserRequestVO sysUserRequestVO = new SysUserRequestVO();
                    sysUserRequestVO.setJobNumber(user.getEmployeeID());
                    sysUserRequestVO.setName(user.getRealName());
                    listUserRequest.add(sysUserRequestVO);
                }
                sysUserService.batchAdd(listUserRequest);
            }
        }
        return true;
    }

    @Override
    public void error(String queueName, String message, Throwable throwable) {

    }


}
